layui.define('conf', function(exports){
	var conf = layui.conf;
	
	utils = {
		parseRouter : function(href){
			href = /^[\/#\.]|^http/.test(href) ? href : "/" + href
			return href.replace(/(\/+)/g, "/");
		},
		sessionData : function(o, value){
			if(typeof(o) === 'object'){
				for(i in o){
					utils.sessionData(i, o[i]);
				}
				return;
			}
			if(utils.isBlank(value)){
				return layui.sessionData(conf.tableName)[o];
			}
			layui.sessionData(conf.tableName, {
				key : o,
				value : value
			})
		},
		data : function(o, value){
			if(typeof(o) === 'object'){
				for(i in o){
					utils.data(i, o[i]);
				}
				return;
			}
			if(utils.isBlank(value)){
				return layui.data(conf.tableName)[o];
			}
			layui.data(conf.tableName, {
				key : o,
				value : value
			})
		},
		//判断对象是否为空
		isBlank :function(o){
			if(o === 0){
				return false;
			}
			if(o === '' || o == null || o  === 'undefined'){
				return true;
			}
			return false;
		},
		//字符串转16进制
		stringToHex : function(str){
			if(utils.isBlank(str)){
				return "";
			}
			var hexCharCode = [];
			for(var i = 0; i < str.length; i++) {
				hexCharCode.push((str.charCodeAt(i)).toString(16));
			}
			return hexCharCode.join("");
		},
		//16进制转字符串
		hexToString : function(hexCharCodeStr){
			if(utils.isBlank(hexCharCodeStr)){
				return "";
			}
			var trimedStr = hexCharCodeStr.trim();
			var rawStr = trimedStr.substr(0, 2).toLowerCase() === "0x" ? trimedStr.substr(2) : trimedStr;
			var len = rawStr.length;
			if (len % 2 !== 0) {
				return "";
			}
			var curCharCode;
			var resultStr = [];
			for (var i = 0; i < len; i = i + 2) {
				curCharCode = parseInt(rawStr.substr(i, 2), 16); // ASCII Code Value
				resultStr.push(String.fromCharCode(curCharCode));
			}
			return resultStr.join("");
		},
		
		//给URL加上参数
		addUrlPara : function(key, value, url){
			url = url || window.location.href;
			var reg = new RegExp("(^|)"+ key +"=([^&]*)(|$)");
			var tmp = key + "=" + value;
			if(url.match(reg) != null){
				return url.replace(eval(reg),tmp);
			}else{
				return url + (url.match("[\?]") ? "&" : "?") + tmp
			}
		},
		
		getUrlPara : function(key, url){
			url = url || layui.router().href;
			if(utils.isBlank(url)){
				return null;
			}
			var reg = new RegExp("[^\?&]?" + encodeURI(key) + "=[^&$]+");
			var r = url.match(reg);
			if (r != null)
				return decodeURI(r[0].substring(r[0].search("=") + 1));
			return null;
		},
		
		getDictData : function(data, field){
			try{
				return data[field] || eval('data.'+field);
			}catch(e){
				
			}
		},
		getCurDate : function(fmat){
			fmat = fmat ? fmat : "YYYY-MM-DD"
			return utils.formatDateTime(new Date(), fmat)
		},
		formatDateTime :function(time, fmat) {
		    if (!time) {
		        return '';
		    }
		    fmat = fmat ? fmat : "YYYY-MM-DD HH:mm:ss";
		    if(typeof time === 'number' || typeof time === 'string'){
		    	return utils.formatDateTime(new Date(parseInt(time)), fmat)
		    }
		    return (time).format(fmat);
		}
		
	}
	
	Date.prototype.format = function(format){
        var o = {
            "M+" : this.getMonth()+1, //month
            "DD+" : this.getDate(), //day
            "HH+" : this.getHours(), //hour
            "m+" : this.getMinutes(), //minute
            "s+" : this.getSeconds(), //second
            "q+" : Math.floor((this.getMonth()+3)/3), //quarter
            "S" : this.getMilliseconds() //millisecond
        }
        if(/(Y+)/.test(format)){
            format=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4 - RegExp.$1.length));
        }
        for(var k in o){
            if(new RegExp("("+ k +")").test(format)){
                format = format.replace(RegExp.$1,RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
            }
        }
        return format;
    }
		
	exports("utils", utils)
});